/****************************************************************************
**
** Copyright (c) 2012 Richard Steffen and/or its subsidiary(-ies).
** All rights reserved.
** Contact: rsteffen@messbild.de, rsteffen@uni-bonn.de
**
** QColorRampEditor is free to use unter the terms of the LGPL 2.1 License in
** Free and Commercial Products.
****************************************************************************/

#ifndef __QCOLORRAMPEDITOR_H__
#define __QCOLORRAMPEDITOR_H__

#include <QWidget>
#include <QColorDialog>

// -----------------------------------------------------------
// QColorRampEditor ------------------------------------------
// -----------------------------------------------------------
class QColorRampEditorSlider;
class QColorRampEditor : public QWidget
{
    Q_OBJECT
public:

    /// Basic Constructor
    QColorRampEditor(QWidget* parent=0);

    /// Destructor
    virtual ~QColorRampEditor();

    /// get the number of sliders
    int getSliderNum();

    /// set a color choose dlg
    void setColorChoose(QColorDialog* coldlg);

    /// set the init color
    void setInitColor(QColor col);

    /// set the update emitting when sliding
    void setSlideUpdate(bool val);

    /// return the Ramp definition
    QVector<QPair<qreal, QColor> > getRamp();

    /// return a 256 colortable from the ramp
    QVector<QRgb> getColorTable();

    /// set Ramp definition
    void setRamp(QVector<QPair<qreal, QColor> > ramp);

    // for DataViewer
    void setMinMaxValue(double min, double max);
    QColor getColorForValue(double value);
    QColor getColorForValueCount(int value, int count);
    QImage getColorRampImage();
    void   drawToPainter(QPainter* painter, QRect rect);

signals:

    /// signal that hide is changed
    void rampChanged();

    /// color request
    void updateSliderColorRequest(int index);

public slots:

    /// set the color of a slider to zero
    void setSliderColor(int index, QColor col);

protected slots:

    /// paint the widget
    virtual void paintEvent(QPaintEvent* e);

    /// resize required
    virtual void resizeEvent (QResizeEvent* e);

    /// detect a mouse is pressed
    virtual void mousePressEvent(QMouseEvent* e);

    /// detect a mouse is moved
    virtual void mouseMoveEvent(QMouseEvent* e);

    /// detect a mouse is released
    virtual void mouseReleaseEvent(QMouseEvent* e);

    /// detect a mouse is released
    virtual void mouseDoubleClickEvent(QMouseEvent* e);

protected:

    /// sort the slider list
    static bool colorRampSort(const QPair<qreal, QColorRampEditorSlider*> &a1, const QPair<qreal, QColorRampEditorSlider*> &a2);

    /// all poses with its sliders
    QList<QPair<qreal, QColorRampEditorSlider*> > sliders_;

    /// the index of the active slider
    int activeSlider_;

    /// a color chooser dlg
    QColorDialog* chooser_;

    /// int color for new Slider
    QColor initColor_;

    /// continous update?
    bool slideUpdate_;

    // for DataViewer

    double m_MinValue;
    double m_MaxValue;
};

// -----------------------------------------------------------
// QColorRampEditorSlider ------------------------------------
// -----------------------------------------------------------
class QColorRampEditorSlider : public QWidget
{
    Q_OBJECT
public:

    /// Constructor
    QColorRampEditorSlider(QWidget* parent=0, QColor col = Qt::black);

    /// set the color of the slider
    void setColor(QColor col);

    /// get the color
    QColor getColor();

    inline void    setText(const QString& text){ text_ = text; }
    inline QString text(){ return text_; }

protected slots:

    /// paint the widget
    virtual void paintEvent(QPaintEvent* event);

protected:

    /// the color of the slider
    QColor  color_;
    QFont   font_;
    QString text_;

};

#endif
